package com.tang.campus_market.controller;

import com.tang.campus_market.entity.Goods;
import com.tang.campus_market.entity.Order;
import com.tang.campus_market.entity.User;
import com.tang.campus_market.service.GoodsService;
import com.tang.campus_market.service.OrderService;
import com.tang.campus_market.service.UserService;
import com.tang.campus_market.util.Result;
import com.tang.campus_market.util.StatusCode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.List;

@RestController
@RequestMapping("/admin")
@CrossOrigin //跨域
public class AdminController {

    @Autowired
    private UserService userService;

    @Autowired
    private GoodsService goodsService;

    @Autowired
    private OrderService orderService;

    /**
     * 管理员登录功能
     * @param email 邮箱
     * @param password 密码
     * @param session 登录成功后设置session
     * @return 返回给前端的json数据
     */
    @RequestMapping(value = "/login",method = RequestMethod.POST)
    public Result AdminLogin(@RequestParam(value = "email",required = false) String email,
                             @RequestParam(value = "password",required = false) String password,
                             HttpSession session){
        //通过邮箱和密码查询用户
        User user = userService.selectUserByEmailAndPassword(email,password);

        if (user != null && "1".equals(user.getPower())) {
            user.setPassword(null);
            session.setAttribute("admin",user);
            return new Result(StatusCode.OK, "管理员登录成功", null);
        } else  {
            //如果没有从数据库查询到该用户，说明邮箱或密码错误
            return new Result(StatusCode.LOGINERROR,"邮箱或密码输入错误",null);
        }
    }


    /**
     * 管理退出
     * @param session
     * @return
     */
    @RequestMapping(value = "/logout",method = RequestMethod.GET)
    public Result AdminLogout(HttpSession session) {
        session.removeAttribute("admin");
        return new Result(StatusCode.OK,"退出成功",null);
    }

    /**
     * 查询所有物品--未分页
     * @return
     */
    @RequestMapping(value = "/goodFindAll",method = RequestMethod.GET)
    public Result AdminFindAllGoods() {
        List<Goods> goodsList = goodsService.findAll("","");
        return new Result(StatusCode.OK,"查询成功",goodsList);
    }

    /**
     * 删除指定goodId的物品，并把该物品对应的评论和订单禁用0
     * @param id 物品的id
     * @return
     */
    @RequestMapping(value = "/goodDelete",method = RequestMethod.GET)
    public Result AdminDeleteGoodById(@RequestParam(value = "id",required = true) Integer id) {
        goodsService.deleteById(id);
        return new Result(StatusCode.OK,"物品删除成功",null);
    }

    /**
     * 禁用指定goodId的物品，并把该物品对应的评论和订单禁用0
     * @param id 物品的id
     * @return
     */
    @RequestMapping(value = "/goodOff",method = RequestMethod.GET)
    public Result AdminOffGoodById(@RequestParam(value = "id",required = true) Integer id) {
        goodsService.updateGoodById(id);
        return new Result(StatusCode.OK,"物品下架成功",null);
    }

    /**
     * 激活指定goodId的物品，并把该物品对应的评论和订单禁用0
     * @param id 物品的id
     * @return
     */
    @RequestMapping(value = "/goodOn",method = RequestMethod.GET)
    public Result AdminOnGoodById(@RequestParam(value = "id",required = true) Integer id) {
        goodsService.updateGoodOnState(id);
        return new Result(StatusCode.OK,"物品上架成功",null);
    }

    /**
     * 查询所有订单--未分页
     * @return
     */
    @RequestMapping(value = "/orderFindAll",method = RequestMethod.GET)
    public Result AdminFindAllOrder() {
        List<Order> ordersList = orderService.findAll();
        return new Result(StatusCode.OK,"查询成功",ordersList);
    }

    /**
     * 删除指定orderId的订单
     * @param id 订单的id
     * @return
     */
    @RequestMapping(value = "/orderDelete",method = RequestMethod.GET)
    public Result AdminDeleteOrderById(@RequestParam(value = "id",required = true) Integer id) {
        orderService.deleteById(id);
        return new Result(StatusCode.OK,"订单删除成功",null);
    }

    /**
     * 查询所有用户--未分页
     * @return
     */
    @RequestMapping(value = "/userFindAll",method = RequestMethod.GET)
    public Result AdminFindAllUser() {
        List<User> userList = userService.findAll();
        return new Result(StatusCode.OK,"查询成功",userList);
    }

    /**
     * 删除指定userId的用户
     * @param id
     * @return
     */
    @RequestMapping(value = "/userDelete",method = RequestMethod.GET)
    public Result AdminDeleteUserById(@RequestParam(value = "id",required = true) Integer id) {
        userService.deleteUserById(id);
        return new Result(StatusCode.OK,"用户删除成功",null);
    }

    /**
     * 禁用指定userId的用户
     * @param id
     * @return
     */
    @RequestMapping(value = "/userForbidden",method = RequestMethod.GET)
    public Result AdminForbidUserById(@RequestParam(value = "id",required = true) Integer id) {
        userService.updateUserById(id);
        return new Result(StatusCode.OK,"用户禁用成功",null);
    }

    /**
     * 激活指定userId的用户
     * @param id
     * @return
     */
    @RequestMapping(value = "/userActive",method = RequestMethod.GET)
    public Result AdminActiveUserById(@RequestParam(value = "id",required = true) Integer id) {
        userService.activeUserById(id);
        return new Result(StatusCode.OK,"用户激活成功",null);
    }


}
